/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package allthemoneyflowers.dao;

import allthemoneyflowers.model.User;
import java.io.File;
import java.util.ArrayList;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

/**
 *
 * @author wolf
 */
//H DOULEIA TIS ReadXML einai na diabazei to XML kai na epistrefei ena Array apo Users ston Controller
public class ReadXML {
    private User user = new User();
    private ArrayList<User> userList = new ArrayList<User>();
           
    public ArrayList<User> readFileXML(){
        
        try 
	{  
            File fXmlFile = new File("C:\\Users\\wolf\\Documents\\NetBeansPreoject\\trunk\\src\\allthemoneyflowers\\dao\\LoginXML.xml"); //To arxeio XML pou exw
			
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(fXmlFile);
			
            doc.getDocumentElement().normalize();//bazei se mia "seira" ta elements
		 	
            //elenxei an exei ginei lathos sto xml(an edwses lathos tag)
            if(doc.getDocumentElement().getNodeName() == "users")
            {	
                NodeList nList = doc.getElementsByTagName("user");//pernei tous users		 					 
                for (int temp = 0; temp < nList.getLength(); temp++)//kanei prospelash olous tous user pou exei i lista 
                {
                    user = new User(); //Mporei na min xreiazete den eimai sigouros
                    Node nNode = nList.item(temp);											 
                    if (nNode.getNodeType() == Node.ELEMENT_NODE) //elenxei an einai swsta domimena ta userNode
                    {
                        Element eElement = (Element) nNode;
			 			
                        //pernei tis times apo to XML kai ta bazeis stin User Clasi
                        user.setUserID(eElement.getAttribute("id"));
                        user.setUsername(eElement.getElementsByTagName("username").item(0).getTextContent());
                        user.setPassword(eElement.getElementsByTagName("password").item(0).getTextContent());						
						
                        //userList.add(user);
                         if(user.getUserID()!=null)
                         {
                            userList.add(user);
                            //System.out.printf(user.getUsername());
                         }
                         else {
                            System.out.printf("Error!!");
                         }
                     }
                  }
                }
                else
                {
                    System.out.printf("Wrong Node!!" +doc.getDocumentElement().getNodeName());
		}
	} 
            catch (Exception e) 
	{
            e.printStackTrace();//an dn mporei na kanei sindesi me to XML
	}
            return userList;
    }
}
        
    
    
  


    

